package com.yy.jhl.ui.user.activity;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.yy.common.base.BaseActivity;
import com.yy.common.commonutils.LogUtils;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by libingn on 2017/12/12.
 */

public class ContactListActivity extends BaseActivity implements AdapterView.OnItemClickListener{
    @BindView(R.id.pull_list_view)
    PullToRefreshListView pullListView;
    ListView contactListView;
    SimpleAdapter simpleAdapter;
    private List<Map<String,String>> mp=new ArrayList<>();
    @Override
    public int getLayoutId() {
        return R.layout.act_pull_list_view;
    }

    @Override
    public void initView() {
        showTitle("手机联系人", new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                finish();
            }
        });
        contactListView = pullListView.getRefreshableView();
        pullListView.setOnItemClickListener(this);
        pullListView.setMode(PullToRefreshBase.Mode.DISABLED);
        getContactsList();
    }


    /**
     * 获取联系人列表
     */
    public void getContactsList(){
        ContentResolver cr = getContentResolver();
        Uri uri= Uri.parse("content://com.android.contacts/raw_contacts");
        Cursor cs=cr.query(uri,null,null,null,null);
        while(cs.moveToNext()){
            //拿到联系人id 跟name
            int id=cs.getInt(cs.getColumnIndex("_id"));
            String name=cs.getString(cs.getColumnIndex("display_name"));
            //得到这个id的所有数据（data表）
            Uri uri1=Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data");
            Cursor cs2=cr.query(uri1,null,null,null,null);
            Map<String,String>  maps=new HashMap<>();//实例化一个map
            while ( cs2.moveToNext()){
                //得到data这一列 ，包括很多字段
                String data1=cs2.getString(cs2.getColumnIndex("data1"));
                //得到data中的类型
                String type=cs2.getString(cs2.getColumnIndex("mimetype"));
                String str=type.substring(type.indexOf("/")+1,type.length());//截取得到最后的类型
                if("name".equals(str)){//匹配是否为联系人名字
                    maps.put("name",data1);
                }if("phone_v2".equals(str)){//匹配是否为电话
                    maps.put("phone",data1);
                }
                LogUtils.loge("getContactsList",data1+"       "+type);
            }
            mp.add(maps);//将map加入list集合中
        }
        //实例一个简易适配器
        simpleAdapter = new SimpleAdapter(this,mp, R.layout.item_contact,new String[]{"name","phone"},new int[]{R.id.contact_name_text,R.id.contact_tel_text} );
        //给listView设置适配器
        contactListView.setAdapter(simpleAdapter);
        simpleAdapter.notifyDataSetChanged();//通知适配器发生数据改变
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        HashMap<String,String> item = (HashMap<String,String>)simpleAdapter.getItem(i-1);
        Intent intent = new Intent();
        intent.putExtra("tel", item.get("phone"));
        setResult(RESULT_OK, intent);
        finish();
    }
}
